﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data.Entity;

namespace SoGame.Model
{
    public partial class SoGameDbContext : DbContext
    {
        /// <summary>
        /// Gets all players.
        /// </summary>
        /// <returns></returns>
        public IQueryable<Player> GetAllPlayers() {
            return this.Players.Select(p=>p).OrderBy(p=> string.Concat(p.Name, " ", p.FirstName));
        }

        /// <summary>
        /// Gets the top10 players.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <returns></returns>
        public IQueryable<Player> GetTop10Players(string game) {
            //var players = from p in this.Players
            //              join g in this.GamePlays on p.ID equals g.PlayerID
            //              where g.Game == game
            //              orderby g.Score descending
            //              select p;

            //return this.Players.Take(10);
            return null;
        }

        /// <summary>
        /// Finds the player.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public IQueryable<Player> FindPlayer(string query) {
            query = query.RemoveDiacritics().ToLower();
            var player = from p in this.Players
                         where string.Concat(p.Name, " ", p.FirstName).Contains(query)
                         select p;

            return player;
        }

        /// <summary>
        /// Saves the player.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns></returns>
        public bool SavePlayer(Player player) {
            this.Players.Add(player);
            return this.SaveChanges() > 0;
        }

        /// <summary>
        /// Gets the player.
        /// </summary>
        /// <param name="playerId">The player id.</param>
        /// <returns></returns>
        public Player GetPlayer(int playerId) {
            return this.Players.SingleOrDefault(p => p.ID == playerId);
        }

        /// <summary>
        /// Deletes the player.
        /// </summary>
        /// <param name="playerId">The player id.</param>
        public void DeletePlayer(int playerId) {
            var player = this.Players.SingleOrDefault(p => p.ID == playerId);

            if (player != null)
            {
                this.Players.Remove(player);
                this.SaveChanges();
            }
        }

        /// <summary>
        /// Updates the player.
        /// </summary>
        /// <param name="player">The player.</param>
        public void UpdatePlayer(Player player) {
            this.Entry(player).State = System.Data.EntityState.Modified;
            this.SaveChanges();
        }

        /// <summary>
        /// Attaches the game play to the player.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="gameplay">The gameplay.</param>
        public void AttachGamePlayToPlayer(Player player, GamePlay gameplay) {
            gameplay.PlayerID = player.ID;

            this.UpdateGamePlay(gameplay);
        }

    }
}
